package com.wzc.basic.service.impl;

import com.wzc.basic.mapper.BaseMapper;
import com.wzc.basic.query.BaseQuery;
import com.wzc.basic.service.IBaseService;
import com.wzc.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {
    @Autowired // 注解注入mapper
    private BaseMapper<T> baseMapper;

    @Override
    public T loadById(Long id) {
        return baseMapper.loadById(id);
    }

    @Override
    public List<T> loadAll() {
        return baseMapper.loadAll();
    }

    @Override
    @Transactional
    public void remove(Long id) {
        baseMapper.remove(id);
    }

    @Override
    @Transactional
    public void save(T T) {
        baseMapper.save(T);
    }

    @Override
    @Transactional
    public void update(T T) {
        baseMapper.update(T);
    }

    @Override
    public PageList<T> queryPage(BaseQuery query) {
        Integer count = baseMapper.queryCount(query); // 分页前查询总条数
//        if (count == null || count ==0) { // 当一条数据都没有（不是第一页）的时候给页面返回一个空数据
//            return new PageList<T>();
//        }
//        // 有数据时，查询所有数据
        List<T> list = baseMapper.queryData(query);
        return new PageList<T>(count,list);
    }

    @Override
    @Transactional
    public void patchDelete(List<Long> ids) {
        baseMapper.patchDelete(ids);
    }


}
